package com.zsc.newbuds.utils;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSBuilder;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import com.zsc.newbuds.config.OssConfig;
import com.zsc.newbuds.entity.Admin;
import com.zsc.newbuds.entity.User;
import com.zsc.newbuds.mapper.UserMapper;
import com.zsc.newbuds.service.impl.AdminServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Random;
import java.util.UUID;

/**
 * 阿里云OSS工具类
 * @author D
 */
@Component
public class OssUtil {

    @Autowired
    RedisUtil redisUtil;

    @Autowired
    AdminServiceImpl adminService;

    @Autowired
    UserMapper userMapper;

    @Autowired
    OssConfig ossConfig;

    private OSS getClient(){
        OSS client = new OSSClientBuilder().build(ossConfig.getEndpoint(),ossConfig.getAccessKeyId(),ossConfig.getAccessKeySecret());
        if(client==null){
            System.out.println("错误");
        }
        return client;
    }

    /**
     * 上传头像
     * @param multipartFile
     * @param type
     * @param id
     * @throws IOException
     */
    public String upLoad(MultipartFile multipartFile,String type,Integer id)throws IOException {

        OSS client = getClient();

        String fileName = multipartFile.getOriginalFilename();
        //后缀名
        assert fileName != null;
        String suffix = fileName.substring(fileName.lastIndexOf("."));
        String prefix="";
        String imageUrl="";
        if("goods".equals(type)){
            prefix = type+"Image/"+UUID.randomUUID().toString().substring(0,6);
            fileName = prefix + suffix;
            imageUrl = ossConfig.getPostHost()+fileName;
        }else {
            prefix =type+"/" + id+"/headSculpture/" +UUID.randomUUID().toString().substring(0,6);
            fileName = prefix + suffix;
            imageUrl = ossConfig.getPostHost()+fileName;
            redisUtil.set("headSculpture:"+type+id.toString(),imageUrl);
        }
        PutObjectRequest putObjectRequest = new PutObjectRequest(ossConfig.getBucketName(),fileName, multipartFile.getInputStream());
        if ("user".equals(type)){
            userMapper.updateImage(id,imageUrl);
        }
        assert client != null;
        client.putObject(putObjectRequest);
        client.shutdown();
        return imageUrl;
    }

    public String upLoadImage(InputStream  inputStream, String type, String name) throws IOException {

        OSS client = getClient();

        //前缀
        String prefix ="record/" + type+"/" +name;
        String fileName = prefix + ".png";
        System.out.println(fileName);
        String imageUrl = ossConfig.getPostHost()+fileName;
        PutObjectRequest putObjectRequest = new PutObjectRequest(ossConfig.getBucketName(),fileName, inputStream);
        assert client != null;
        client.putObject(putObjectRequest);
        client.shutdown();
        return imageUrl;
    }

    public String upLoadExcel(InputStream  inputStream) throws IOException {

        OSS client = getClient();

        //前缀
        String prefix ="record/excel/" +UUID.randomUUID().toString().substring(0, 6);
        String fileName = prefix + ".xlsx";
        System.out.println(fileName);
        String excelUrl = ossConfig.getPostHost()+fileName;
        PutObjectRequest putObjectRequest = new PutObjectRequest(ossConfig.getBucketName(),fileName, inputStream);
        assert client != null;
        client.putObject(putObjectRequest);
        client.shutdown();
        return excelUrl;
    }



}
